import { goodsList,goodsCount } from "../../requster/api"
const state={
    list:[],
    total:0,
    size:2,
    page:1,
}
const mutations={
    changelist(state,arr){
        state.list=arr
    },
    changetotal(state,num){
        state.total=num
    },
    changepage(state,num){
        state.page=num

    }
}
const actions={
    reqchangelist(context){
        goodsList({size:context.state.size,page:context.state.page}).then(res=>{
            let list=res.data.list?res.data.list:[]
            if (list.length==0&&context.state.page > 1) {
                    context.commit('changepage',context.state.page-1)
                    context.dispatch('reqchangelist')
            } 
            
            context.commit("changelist", list)
        })
    },
    reqchangepage(context, num) {
        context.commit("changepage", num)
        // 重新请求列表
        context.dispatch("reqchangelist")
    },
    reqchangetotal(context) {
        goodsCount().then(res => {
            context.commit("changetotal", res.data.list[0].total)
        })
    },
}
const getters={
    list(state){
        return state.list
    },
    total(state){
        return state.total
    },
    size(state){
        return  state.size
    }
}
export default{
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}